package test.my.com.myapplicationtest.write_view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Environment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.io.File;

import test.my.com.myapplicationtest.paint_view.BitmapUtil;

/**
 * Created by ThinkPad on 2016/3/17.
 */
public class PaintPicView extends View {

    private Paint mPaint;
    private Bitmap mOriginalBitmap;
    private Path mPath;

    private float mX;
    private float mY;

    private String originalPath;


    //-----------------------------------------------
    Rect mScreenImgRect;
    int mImageWidth;
    int mImageHeight;
    float mRatio;

    public PaintPicView(Context context) {
        super(context);
        init();
    }

    public PaintPicView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PaintPicView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);// 抗锯齿
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);// 始末端圆角
        mPaint.setDither(true); // 防抖动
        mPaint.setStrokeWidth(20);
        mPath = new Path();
        originalPath = Environment.getExternalStorageDirectory() + File.separator + "172.jpg";
        mOriginalBitmap = BitmapFactory.decodeFile(originalPath);
        mImageWidth = mOriginalBitmap.getWidth();
        mImageHeight = mOriginalBitmap.getHeight();
        Log.e("-------------",mImageWidth+"*"+mImageHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(mOriginalBitmap, null, mScreenImgRect, mPaint);
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if(mImageWidth <= 0 || mImageHeight <= 0){
            return;
        }
        int contentWidth = right - left;
        int contentHeight = bottom - top;
        float widthRatio = contentWidth/((float)mImageWidth);
        float heightRatio = contentHeight/((float)mImageHeight);
        mRatio = widthRatio < heightRatio ? widthRatio : heightRatio;
        int realImageWidth = (int)(mImageWidth * mRatio) ;
        int realImageHeight = (int)(mImageHeight *mRatio) ;
        int imageLeft = (contentWidth - realImageWidth)/2;
        int imageTop = (contentHeight - realImageHeight)/2;
        mScreenImgRect = new Rect(imageLeft,imageTop,imageLeft+realImageWidth,imageTop+realImageHeight);
    }

    public void saveDoneImage() {
        String desPath = Environment.getExternalStorageDirectory() + File.separator + "LL" + ".jpg";
        Bitmap tempBitmap = Bitmap.createBitmap(mScreenImgRect.width(), mScreenImgRect.height(),
                Bitmap.Config.ARGB_8888);
        Canvas tempCanvas = new Canvas(tempBitmap);
        tempCanvas.drawColor(Color.GRAY);
        tempCanvas.translate(-mScreenImgRect.left, -mScreenImgRect.top);
        tempCanvas.drawBitmap(mOriginalBitmap, null, mScreenImgRect, mPaint);
        tempCanvas.drawPath(mPath, mPaint);
        tempCanvas.save();
        Matrix matrix = new Matrix();
        matrix.postScale(1 / mRatio, 1 / mRatio);
        Bitmap tempBitmap1 = Bitmap.createBitmap(tempBitmap, 0, 0, tempBitmap.getWidth(), tempBitmap.getHeight(), matrix,
                true);
        BitmapUtil.createImage(tempBitmap1, desPath);
        tempBitmap.recycle();
        tempBitmap1.recycle();
    }


    public void clearPath(){
        if(mPath.isEmpty()){
            Log.e("ah","empty");
        }else{
            Log.e("ah","no");
        }
        mPath.reset();
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float currentX = event.getX();
        float currentY = event.getY();
        if (!mScreenImgRect.contains((int)currentX,(int)currentY)){
            return true;
        }
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float downX = event.getX();
                float downY = event.getY();

                mX = downX;
                mY = downY;

                mPath.moveTo(downX, downY);
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(currentX - mX);
                float dy = Math.abs(currentY - mY);

                //两点之间的距离大于等于3时，生成贝塞尔绘制曲线
                if (dx > 3 || dy > 3) {
                    //设置贝塞尔曲线的操作点为起点和终点的一半
                    float hx = (mX + currentX) / 2;
                    float hy = (mY + currentY) / 2;
                    //二次贝塞尔，实现平滑曲线；mX, mY为操作点，hx, hy为终点
                    mPath.quadTo(mX, mY, hx, hy);
                }
                //第二次执行时，第一次结束调用的坐标值将作为第二次调用的初始坐标值
                mX = currentX;
                mY = currentY;
                break;
            case MotionEvent.ACTION_UP:
                mPath.lineTo(mX, mY);
                break;
        }
        invalidate();
        return true;
    }
}
